let Game = require('TxGame')
let {Global} = require('Global')
let {Texas} = require('Poker')
const LOSE_USER_SIZE = 80
const LOSE_USER_SPANCE = 40

cc.Class({
    extends: cc.Component,
    properties: {
        loserPrefab: cc.Prefab,

        data: {
            visiable: false,
            set(newValue) {
                let children = this.otherPos.getChildren()
                children.forEach((item, idx) => {
                    item.runAction(cc.removeSelf(true))
                })
                let count = newValue.length
                var lidx = 0
                let lose_ori = (count - 2) / 2 * LOSE_USER_SIZE + (count - 2) / 2 * LOSE_USER_SPANCE
                newValue.forEach((item, idx) => {
                    let {playerId, money, name, avatar, k} = item
                    if (playerId === Global.user._id) {
                        if (money > 0) {
                            this._playWin()
                        } else {
                            this._playLose()
                        }
                        this.myResultMoney.string = money.toString()
                        this.myName.string = name
                        this.myHeader.user = item
                    } else {
                        let pos = this.otherPos.getPosition()
                        let loseUser = cc.instantiate(this.loserPrefab)
                        let lpos = new cc.Vec2({ x: - lose_ori + (LOSE_USER_SIZE - 1) * lidx + (LOSE_USER_SPANCE - 1) * lidx, y: -114 })
                        loseUser.setPosition(lpos)
                        loseUser.getComponent('TxLoseUser').info = item
                        this.otherPos.addChild(loseUser)
                        lidx++
                    }
                })
            },
        },

        myResultMoney: cc.Label,
        myName: cc.Label,
        myHeader: cc.Node,
        myCardType: cc.Label,
        flag: cc.Sprite,
        otherPos: cc.Node,
        flag_sprite: {
            default: [],
            type: cc.SpriteFrame
        }
    },

    onLoad() {
        this.myHeader = this.myHeader.getComponent('Actor')
    },

    playAgain() {
        // let count = 4
        // let lose_ori = (count - 1) / 2 * LOSE_USER_SIZE + (count - 1) / 2 * LOSE_USER_SPANCE

        // for (let i = 0; i < count; i++) {
        //     let pos = this.otherPos.getPosition()
        //     let loseUser = cc.instantiate(this.loserPrefab)
        //     let lpos = new cc.Vec2({ x: - lose_ori + (LOSE_USER_SIZE - 1) * i + (LOSE_USER_SPANCE - 1) * i, y: -114 })
        //     loseUser.setPosition(lpos)
        //     //loseUser.getComponent('TxLoseUser').info = item
        //     this.otherPos.addChild(loseUser)
        // }

        let children = this.otherPos.getChildren()
        children.forEach((item, idx) => {
            item.removeFromParent()
        })
        this.node.active = false
    },

    quit() {
        this.node.active = false
    },

    _playLose() {
        this.flag.spriteFrame = this.flag_sprite[0]
    },

    _playWin() {
        this.flag.spriteFrame = this.flag_sprite[1]
    },
})